﻿using LightCAD.Runtime;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LightCAD.UI
{
    public partial class FloatEditWindow : Form, IWindow
    {
        private DocumentRuntime docRt;
        private DrawingEditControl editControl;
        private string activeEditor;
        internal PaperSpaceControl paperSpaceControl;
        internal Model3DEditControl model3DEditControl;
        internal ModelView3DControl modelView3DControl;
        private bool isActive;
        public FloatEditWindow()
        {
            InitializeComponent();
            this.InitDrag();
            this.Activated += FloatEditWindow_Activated; ;
            this.Deactivate += FloatEditWindow_Deactivate;
            this.FormClosed += FloatEditWindow_FormClosed;
        }
        public FloatEditWindow(DocumentRuntime docRt, DrawingEditControl editControl, string editorName) : this()
        {
            this.docRt = docRt;
            this.editControl = editControl;
            this.activeEditor = editorName;
            this.editorSelector.HideAll();
            this.editorSelector.SetVisible(editorName, true);
            var ctrl = this.CreateEditor(editorName);
            CreateTabItem(Path.GetFileName(docRt.Document.FilePath), ctrl);
        }
        private void FloatEditWindow_FormClosed(object? sender, FormClosedEventArgs e)
        {
            if (model3DEditControl != null)
            {
                this.editControl.ShowInHost(this.activeEditor);
            }
        }

       
        private Label CreateTabItem(string text, object tag)
        {
            var lbl = new Label();
            lbl.AutoSize = true;
            lbl.Padding = new Padding(4, 4, 4, 4);
            lbl.Dock = DockStyle.Left;
            lbl.BorderStyle = BorderStyle.FixedSingle;
            lbl.Text = text;
            lbl.Tag = tag;
            pnlHeaders.Controls.Add(lbl);
            lbl.BringToFront();
            return lbl;
        }
        private void FloatEditWindow_Deactivate(object? sender, EventArgs e)
        {
            this.isActive = false;
        }

        private void FloatEditWindow_Activated(object? sender, EventArgs e)
        {
            this.isActive = true;
        }

        public bool IsActive => this.isActive;

        public Type WinType => this.GetType();

        private void InitEditor(string editorName)
        {
            if (editorName == "Paper")
            {

            }
            else if (editorName == "Model3D")
            {

            }
            else if (editorName == "View3D")
            {

            }
        }

        private Control CreateEditor(string editorName)
        {
            for (var i = 0; i < this.pnlEditors.Controls.Count; i++)
            {
                var ctrl = this.pnlEditors.Controls[i];
                if (ctrl.Name == editorName) return ctrl;
            }
            Control editorCtrl = null;
            switch (editorName)
            {
                case "Paper":
                    this.paperSpaceControl = new PaperSpaceControl();
                    editorCtrl = this.paperSpaceControl;
                    break;
                case "Model3D":
                    this.model3DEditControl = new Model3DEditControl();
                    editorCtrl = this.model3DEditControl;
                    break;
                case "View3D":
                    this.model3DEditControl = new Model3DEditControl();
                    editorCtrl = this.model3DEditControl;
                    break;
            }
            editorCtrl.Dock = DockStyle.Fill;
            editorCtrl.Name = editorName;
            this.pnlEditors.Controls.Add(editorCtrl);
            this.InitEditor(editorName);
            return editorCtrl;
        }


        #region 'Drag'

        int posX;
        int posY;
        int left;
        int top;
        bool drag;
        bool maximized;



        private void InitDrag()
        {
            buttonClos.Click += buttonClos_Click;
            buttonMax.Click += buttonMax_Click;
            buttonMin.Click += buttonMin_Click;
            panel1.MouseDoubleClick += panel1_MouseDoubleClick;
            panel1.MouseDown += panel1_MouseDown;
            panel1.MouseMove += panel1_MouseMove;
            panel1.MouseUp += panel1_MouseUp;
        }
        private void buttonClos_Click(object sender, EventArgs e)
        {
            var form = this;
            form.Close();
        }

        private void buttonMax_Click(object sender, EventArgs e)
        {
            var form = this;
            if (maximized)
            {
                maximized = false;
                form.WindowState = FormWindowState.Normal;
            }
            else
            {
                maximized = true;
                form.WindowState = FormWindowState.Maximized;
            }
        }

        private void buttonMin_Click(object sender, EventArgs e)
        {
            var form = this;
            form.WindowState = FormWindowState.Minimized;
        }
        private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                var form = this;
                if (maximized)
                {
                    form.WindowState = FormWindowState.Normal;
                    maximized = false;
                }
                else
                {
                    form.WindowState = FormWindowState.Maximized;
                    maximized = true;
                }
            }
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            var form = this;
            if (e.Button == MouseButtons.Left)
            {
                drag = true;
                posX = Cursor.Position.X;
                posY = Cursor.Position.Y;
                left = form.Left;
                top = form.Top;
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            drag = false;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            var form = this;
            if (drag)
            {
                form.Top = this.top + (Cursor.Position.Y - posY);
                form.Left = this.left + (Cursor.Position.X - posX);
            }
            form.Cursor = Cursors.Default;
        }

        #endregion
    }
}
